package com.area.cth.controller;

import com.area.cth.pojo.Category;
import com.area.cth.common.Result;
import com.area.cth.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "物品分类相关接口")
@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService cs;

    @Operation(summary = "新增物品分类")
    @PostMapping
    public Result addC(@RequestBody @Validated(Category.Add.class) Category cg) {
        cs.add(cg);
        return Result.success();
    }

    @Operation(summary = "查询全部物品分类")
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> csl = cs.getl();
        return Result.success(csl);
    }

    @Operation(summary = "根据id查询物品分类")
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {
        Category c = cs.findById(id);
        return Result.success(c);
    }

    @Operation(summary = "修改物品分类")
    @PutMapping
    public Result updateC(@RequestBody @Validated(Category.Update.class) Category cg) {
        cs.updateC(cg);
        return Result.success();
    }

    @Operation(summary = "删除物品分类")
    @DeleteMapping
    public Result delete(Integer id) {
        cs.delete(id);
        return Result.success();
    }
}
